//
//  CZStatusCell.swift
//  weibo27
//
//  Created by mac on 16/4/19.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

class CZStatusCell: UITableViewCell {

    //cell中的模型属性
    var statusViewModel:CZStatusViewModel?{
        didSet{
            
            topView.statusViewModel = statusViewModel
            
            textView.text = statusViewModel?.userStatus.text
            
            picture.statusViewModel = statusViewModel
            //传过去后根据sizeToFize()返回回来一些具体的高和宽
            picture.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(picture.bounds.height)
                // 直接设置宽度数值
                make.width.equalTo(picture.bounds.width)
                
            }
        }
    }
    
    /// 根据指定的视图模型计算行高
    ///
    /// - parameter vc: 视图模型
    ///
    /// - returns: 返回模型对应的行高
    func rowHeight(vc:CZStatusViewModel)->CGFloat{
        
        statusViewModel = vc
        //强制跟新约束
        contentView.layoutIfNeeded()
        
        //返回最下面的高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 懒加载控件
    private lazy var topView:CZStatusTop = CZStatusTop()
    
    //内容标签
     lazy var textView:UILabel = UILabel(title: "微博正文", fontSize: 15, color: UIColor.blackColor(),screenInset: StatusCellMargin)
    
    //中间的配图
     lazy var picture:CZStatusPicture = CZStatusPicture()
    
    //底部视图
     lazy var bottomView:CZStatusBottom = CZStatusBottom()

}

// MARK: 布局
extension CZStatusCell{
    
    func setupUI(){
        
        //添加控件
        contentView.addSubview(topView)
        contentView.addSubview(textView)
        contentView.addSubview(bottomView)
        contentView.addSubview(picture)
//        bottomView.backgroundColor = UIColor.redColor()
        
        //设置frame
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
            
            
        }
//        //内容的标签
        textView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)

            
        }
        
        //图片数组
//        picture.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(textView.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(textView.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//
//        }
        // 4> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(picture.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
        
    }
}














